home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 295_01.zip / BOPEN.C < prev    next >
Text File  |  1993-04-22  |  4KB  |  172 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bopen.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. /*#include <string.h>*/
  9. #include "blkio_.h"
  10.  
  11. /* block file control structure table definition */
  12. BLKFILE biob[BOPEN_MAX];
  13.  
  14. /*man---------------------------------------------------------------------------
  15. NAME
  16.      bopen - open a block file
  17.  
  18. SYNOPSIS
  19.      #include <blkio.h>
  20.  
  21.      BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
  22.      const char *filename;
  23.      const char *type;
  24.      size_t hdrsize;
  25.      size_t blksize;
  26.      size_t bufcnt;
  27.  
  28. DESCRIPTION
  29.      The bopen function opens the file named by filename as a block
  30.      file.  A pointer to the BLKFILE structure associated with file is
  31.      returned.
  32.  
  33.      filename points to a character string that contains the name of
  34.      the file to be opened.
  35.  
  36.      type is a character string having one of the following values:
  37.  
  38.           "r"            open for reading
  39.           "r+"           open for update (reading and writing)
  40.           "w+"           truncate or create for update
  41.           "c"            create for update
  42.  
  43.      If type is "r" or "r+" and the file does not exist, bopen will
  44.      fail.  If type is "c" and the file already exists, bopen will
  45.      fail.
  46.  
  47.      hdrsize is the size of the file header.  If there is no file
  48.      header, specify a value of 0 for hdrsize.
  49.  
  50.      blksize is the size of the blocks.
  51.  
  52.      bufcnt is the number of blocks to for which to create buffer
  53.      storage.  For unbuffered operation, specify a value of 0 for
  54.      bufcnt.
  55.  
  56.      bopen will fail if one or more of the following is true:
  57.  
  58.      [EEXIST]       type is "c" and the named file exists.
  59.      [EINVAL]       filename or type is the NULL pointer.
  60.      [EINVAL]       type is not "r", "r+", "w+", or "c".
  61.      [EINVAL]       blksize is 0.
  62.      [ENOENT]       type is "r" or "r+" and the named file
  63.                     does not exist.
  64.      [BEMFILE]      The maximum number of block files is
  65.                     already open.
  66.  
  67. SEE ALSO
  68.      bclose.
  69.  
  70. DIAGNOSTICS
  71.      bopen returns a NULL pointer on failure, and errno is set to
  72.      indicate the error.
  73.  
  74. NOTES
  75.      If the file being opened does not end on a block boundary, the
  76.      trailing partial block is ignored; blkio considers the end of the
  77.      last complete block as the end of the file.
  78.  
  79. ------------------------------------------------------------------------------*/
  80. BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
  81. CONST char *filename;
  82. CONST char *type;
  83. size_t hdrsize;
  84. size_t blksize;
  85. size_t bufcnt;
  86. {
  87.     BLKFILE *bp = NULL;
  88.     int terrno = 0;
  89.  
  90.     /* validate arguments */
  91.     if ((filename == NULL) || (type == NULL) || (blksize == 0)) {
  92.         errno = EINVAL;
  93.         return NULL;
  94.     }
  95.  
  96.     /* find free slot in biob table */
  97.     for (bp = biob; bp < (biob + BOPEN_MAX); bp++) {
  98.         if (!(bp->flags & BIOOPEN)) {
  99.             break;        /* found */
  100.         }
  101.     }
  102.     if (bp > (biob + BOPEN_MAX - 1)) {
  103.         errno = BEMFILE;
  104.         return NULL;        /* no free slots */
  105.     }
  106.  
  107.     /* set biob flags */
  108.     if (strcmp(type, BF_READ) == 0) {
  109.         bp->flags = BIOREAD;
  110.     } else if (strcmp(type, BF_RDWR) == 0) {
  111.         bp->flags = BIOREAD | BIOWRITE;
  112.     } else if (strcmp(type, BF_CREATE) == 0) {
  113.         bp->flags = BIOREAD | BIOWRITE;
  114.     } else if (strcmp(type, BF_CRTR) == 0) {
  115.         bp->flags = BIOREAD | BIOWRITE;
  116.     } else {
  117.         errno = EINVAL;
  118.         return NULL;
  119.     }
  120.  
  121.     /* open file */
  122.     if (b_uopen(bp, filename, type) == -1) {
  123.         if ((errno != EEXIST) && (errno != ENOENT)) BEPRINT;
  124.         memset(bp, 0, sizeof(*biob));
  125.         bp->flags = 0;
  126.         return NULL;
  127.     }
  128.  
  129.     /* initialize */
  130.     bp->hdrsize = hdrsize;
  131.     bp->blksize = blksize;
  132.     bp->bufcnt = bufcnt;
  133.     bp->endblk = 0;
  134.     bp->most = 0;
  135.     bp->least = 0;
  136.     bp->block_p = NULL;
  137.     bp->blkbuf = NULL;
  138.     if (b_uendblk(bp, &bp->endblk) == -1) {
  139.         BEPRINT;
  140.         terrno = errno;
  141.         b_uclose(bp);
  142.         memset(bp, 0, sizeof(*biob));
  143.         bp->flags = 0;
  144.         errno = terrno;
  145.         return NULL;
  146.     }
  147.     /* allocate memory for bp */
  148.     if (b_alloc(bp) == -1) {
  149.         BEPRINT;
  150.         terrno = errno;
  151.         b_uclose(bp);
  152.         memset(bp, 0, sizeof(*biob));
  153.         bp->flags = 0;
  154.         errno = terrno;
  155.         return NULL;
  156.     }
  157.     /* initialize buffer storage */
  158.     if (b_initlist(bp) == -1) {
  159.         BEPRINT;
  160.         terrno = errno;
  161.         b_free(bp);
  162.         b_uclose(bp);
  163.         memset(bp, 0, sizeof(*biob));
  164.         bp->flags = 0;
  165.         errno = terrno;
  166.         return NULL;
  167.     }
  168.  
  169.     errno = 0;
  170.     return bp;
  171. }
  172.